﻿#pragma once
// 枚举值表⽰颜⾊
enum Colour
{
	RED,
	BLACK
};

template<class K, class V>
struct RBTreeNode//红黑树
{
	// 这⾥更新控制平衡也要加⼊parent指针
	pair<K, V> _kv;
	RBTreeNode<K, V>* _left;
	RBTreeNode<K, V>* _right;
	RBTreeNode<K, V>* _parent;
	Colour _col;
	RBTreeNode(const pair<K, V>& kv)
		:_kv(kv)
		, _left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
	{
	}
};

template<class K, class V>
class RBTree
{
	typedef RBTreeNode<K, V> Node;
public:
	bool Insert(const pair<K, V>& kv)//插入一个pair值
	{
		if (_root == nullptr)//让这个kv成为根节点
		{
			_root = new Node(kv);
			_root->_col = BLACK;//根节点颜色是黑色的
			return true;
		}

		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_kv.first < kv.first)//如果此时的这个节点小于插入节点的值的话，那么就往右边走
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_kv.first > kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				return false;
			}
		}
		cur = new Node(kv);
		cur->_col = RED;
		if (parent->_kv.first < kv.first)
		{
			parent->_right = cur;
		}
		else
		{
			parent->_left = cur;
		}

		cur->_parent = parent;


		while (parent && parent->_col == RED)//如果父亲存在并且是红色的话
		{
			//我们就得看叔叔了
			Node* grandfather = parent->_parent;//爷爷
			if (grandfather->_left == parent)
			{
				Node* uncle = grandfather->_right;
				if (uncle && uncle->_col == RED)//叔叔存在并且颜色为红
				{
					//变色操作
					parent->_col == uncle->_col = BLACK;//将父亲和叔叔的颜色变成黑色
					grandfather->_col = RED;//将爷爷变成红

					//继续往上面进行处理操作
					cur = grandfather;
					parent = cur->_parent;
				}
				else//grandfather->_right == parent
				{
					//叔叔不存在，或者存在且为黑
					if (cur == parent->_left)
					{
						//单旋转+变色
						//   g
						// p   u
						//c
						RotateR(grandfather)//右单旋,从爷爷的位置开始右单旋
							parent->_col = BLACK;//将父亲变黑
						grandfather->_col = RED;//将爷爷变红
						//   p
						//c     g
						//        u

					}
					else
					{
						//双旋+变色
						//    g
						//  p   u
						//    c
						RotateL(parent);//先以parent为首进行一个左单旋
						RotateR(grandfather);//再以爷爷为首进行一个右单旋
						cur->_col = BLACK;//将cur变黑
					}
					break;
				}



			}
			else
			{
				//  g
				//u   p
				Node* uncle = grandfather->_left;
				//叔叔存在并且是红的，变色就行了
				if (uncle && uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;

					//继续往上面处理
					cur = grandfather;
					parent = cur->_parent;
				}
				else//叔叔不存在，或者存在且为黑
				{
					//旋转+变色
					//   g
					//u     p
					//         c
					if (cur == parent->_right)
					{
						RotateL(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					else
					{
						//    g
						//  u    p
						//    c
						RotateR(parent);
						RotateL(grandfather);
						cur->_col = BLACK;
						grandfather->_col = RED;
					}
					break;
				}
			}
		}
		_root->_col = BLACK;//始终将根变成黑色
		return true;
	}
	void InOrder()
	{
		_InOrder(_root);
		cout << endl;
	}
	bool Check(Node* root, int blackNum, const int refNum)
	{
		//我们这里根据性质进行判断操作
		if (root == nullptr)
		{
			// 前序遍历⾛到空时，意味着⼀条路径⾛完了
			//cout << blackNum << endl;
			if (refNum != blackNum)
			{
				cout << "存在⿊⾊结点的数量不相等的路径" << endl;
				return false;
			}
			return true;
		}
		// 检查孩⼦不太⽅便，因为孩⼦有两个，且不⼀定存在，反过来检查⽗亲就⽅便多了
		if (root->_col == RED && root->_parent->_col == RED)
		{
			cout << root->_kv.first << "存在连续的红⾊结点" << endl;
			return false;
		}
		if (root->_col == BLACK)
		{
			blackNum++;
		}
		return Check(root->_left, blackNum, refNum)
			&& Check(root->_right, blackNum, refNum);
	}
	bool IsBalance()
	{
		if (_root == nullptr)return true;
		if(_root->_col==RED)return false
		while (cur)
		{
			if (cur->_col == BLACK)
			{
				++refNum;
			}
			cur = cur->_left;
		}
		return Check(_root, 0, refNum);
	}




	private:
		Node* _root = nullptr;
	
};